﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using HRProject.Code.DTO;
using System.Collections.Generic;
using System.Globalization;

namespace HRProject.Worktime
{
    public partial class EditWorktime : System.Web.UI.Page
    {
        private Guid agentId;
        private DateTime date;
        private BLWorktimeRecord worktimeRecord;

        public BLWorktimeRecord WorktimeRecord
        {
            get { return worktimeRecord; }
            set { worktimeRecord = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            btnOK.Text = ContentProvider.GetString("Update");
            try
            {
                agentId = new Guid(Request.Params["agentId"]);
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
                Util.CloseDialog(Response);
                return;
            }
            try
            {
                date = DateTime.ParseExact(Request.Params["date"], "yyyyMMdd", Constant.enCulture);
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
                Util.CloseDialog(Response);
                return;
            }

            //if (!IsPostBack)
            {
                WorktimeRecordDAO worktimeRecordDao = new WorktimeRecordDAO();
                BLWorktimeProperty propertyFilter = new BLWorktimeProperty(agentId, date);
                worktimeRecord = worktimeRecordDao.Search(propertyFilter);
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            AuditDAO auditDao = new AuditDAO();
            BLAudit audit = auditDao.Get(BLAuthentication.CurrentUserId, "work");
            bool authenticated = false;
            foreach (BLAuditMode mode in audit.Modes)
            {
                if ("worktime".Equals(mode.ModeKey))
                {
                    authenticated = true;
                    break;
                }
            }
            if (!authenticated)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorizedDetail").Replace("{0}", ContentProvider.GetString("Worktime"));
                return;
            }

            try
            {
                agentId = new Guid(Request.Params["agentId"]);
                date = DateTime.ParseExact(Request.Params["date"], "yyyyMMdd", Constant.enCulture);
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
                return;
            }

            Guid propertyId;
            try
            {
                propertyId = new Guid(Request.Params["hdfWorktimePropertyId"]);
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
                propertyId = Guid.NewGuid();
            }

            string[] worktimeIds = Request.Params.GetValues("hdfWorktimeId");
            string[] startHours = Request.Params.GetValues("cbxStartHour");
            string[] startMinutes = Request.Params.GetValues("cbxStartMinute");
            string[] endHours = Request.Params.GetValues("cbxEndHour");
            string[] endMinutes = Request.Params.GetValues("cbxEndMinute");
            WorktimeDAO worktimeDao = new WorktimeDAO();
            if ((worktimeIds != null) && (worktimeIds.Length > 0))
            {
                for (int i = 0; i < worktimeIds.Length; i++)
                {
                    BLWorktime worktime = new BLWorktime();
                    bool isNew = false;
                    try
                    {
                        worktime.Id = new Guid(worktimeIds[i]);
                        if (worktime.Id.Equals(Guid.Empty))
                        {
                            throw new Exception();
                        }
                    }
                    catch (Exception exp)
                    {
                        worktime.Id = Guid.NewGuid();
                        isNew = true;
                        Console.Out.WriteLine(exp.Message);
                    }
                    try
                    {
                        worktime.AgentId = agentId;
                        worktime.StartTime = new DateTime(date.Year, date.Month, date.Day,
                            Convert.ToInt32(startHours[i]), Convert.ToInt32(startMinutes[i]), 0);
                        worktime.EndTime = new DateTime(date.Year, date.Month, date.Day,
                            Convert.ToInt32(endHours[i]), Convert.ToInt32(endMinutes[i]), 0);
                        int worktimeResult = 0;
                        if (isNew)
                        {
                            worktimeResult = worktimeDao.insert(worktime);
                        }
                        else
                        {
                            worktimeResult = worktimeDao.update(worktime);
                        }
                    }
                    catch (Exception exp)
                    {
                        Console.Out.WriteLine(exp.Message);
                    }
                }
            }

            string[] removedWorktimeIds = Request.Params.GetValues("hdfRemovedWorktimeId");
            if ((removedWorktimeIds != null) && (removedWorktimeIds.Length > 0))
            {
                for (int i = 0; i < removedWorktimeIds.Length; i++)
                {
                    BLWorktime worktime = new BLWorktime();
                    try
                    {
                        worktime.Id = new Guid(removedWorktimeIds[i]);
                        worktimeDao.delete(worktime);
                    }
                    catch (Exception exp)
                    {
                        Console.Out.WriteLine(exp.Message);
                    }
                }
            }

            BLWorktimeProperty property = new BLWorktimeProperty(agentId, date);
            bool isNew2 = false;
            try
            {
                property.Id = new Guid(Request.Params["hdfWorktimePropertyId"]);
                if (property.Id.Equals(Guid.Empty))
                {
                    throw new Exception();
                }
            }
            catch (Exception exp)
            {
                property.Id = Guid.NewGuid();
                isNew2 = true;
                Console.Out.WriteLine(exp.Message);
            }
            property.Description = Request.Params["txaDescription"];
            if ("1".Equals(Request.Params["ckbIsApproved"]))
            {
                property.Approved = 3;
            }
            WorktimePropertyDAO propertyDao = new WorktimePropertyDAO();
            int result = 0;
            if (isNew2)
            {
                result = propertyDao.insert(property);
            }
            else
            {
                result = propertyDao.update(property);
            }

            if (result > 0)
            {
                Util.CloseDialogWithoutRefresh(Response);
            }
            else
            {
                lblError.Text = ContentProvider.GetMessage("UnsuccessfullyUpdated");
            }
        }
    }
}
